﻿'
' Will Strohl (will.strohl@gmail.com)
' http://www.willstrohl.com
' http://dnntweet.codeplex.com
'
' Copyright (c) 2010, Will Strohl
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this 
' software and associated documentation files (the "Software"), to deal in the Software 
' without restriction, including without limitation the rights to use, copy, modify, 
' merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 
' permit persons to whom the Software is furnished to do so, subject to the following 
' conditions:
'
' The above copyright notice and this permission notice shall be included in all copies 
' or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
' INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
' PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
' HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
' OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'

Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Entities.Modules
Imports System.Text
Imports System.Web
Imports System.Xml

Namespace WillStrohl.Modules.Twitter

    Public NotInheritable Class TwitterController
        Implements IPortable

#Region " Constants "

        Public Const HOSTED_JQUERY_URL As String = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"
        Public Const HOSTED_JQUERY_DEBUG_URL As String = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"
        Public Const HOSTED_JQUERY_UI_URL As String = "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js"
        Public Const HOSTED_JQUERY_UI_DEBUG_URL As String = "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.js"

        Public Const CSS_TAG_INCLUDE_FORMAT As String = "<link rel=""stylesheet"" type=""text/css"" href=""{0}"" />"
        Public Const SCRIPT_TAG_FORMAT As String = "<script language=""javascript"" type=""text/javascript"">{0}</script>"
        Public Const SCRIPT_TAG_INCLUDE_FORMAT As String = "<script language=""javascript"" type=""text/javascript"" src=""{0}""></script>"

        Public Const OAUTH_TOKEN_KEY As String = "oauth_token"
        Public Const OAUTH_VERIFIER_KEY As String = "oauth_verifier"
        Public Const TWITTER_CONSUMER_KEY As String = "twitter.consumer.key"
        Public Const TWITTER_CONSUMER_SECRET_KEY As String = "twitter.consumer.secret"
        Public Const TWITTER_SEARCH_TERMS_KEY As String = "twitter.search.terms"
        Public Const TWITTER_REFRESH_RATE_KEY As String = "twitter.refresh.rate"
        Public Const JQUERY_HOSTED_KEY As String = "jquery.hosted"
        Public Const JQUERY_HOSTED_URL_KEY As String = "jquery.hosted.url"
        Public Const JQUERYUI_HOSTED_KEY As String = "jqueryui.hosted"
        Public Const JQUERYUI_HOSTED_URL_KEY As String = "jqueryui.hosted.url"
        Public Const JQUERYUI_THEME_KEY As String = "jqueryui.theme"
        Public Const SETTINGS_GLOBAL As String = "settings.global"

        Public Const TWITTER_REQUEST_TOKEN_URL As String = "http://twitter.com/oauth/request_token"
        Public Const TWITTER_ACCESS_TOKEN_URL As String = "http://twitter.com/oauth/access_token"
        Public Const TWITTER_AUTHORIZE_URL As String = "http://twitter.com/oauth/authorize"

#End Region

#Region " Data Access "

        Public Function AddSearchHistory(ByVal ModuleId As Integer, ByVal UserId As Integer, ByVal SearchText As String) As Integer
            Return DataProvider.Instance().AddSearchHistory(ModuleId, UserId, SearchText)
        End Function

        Public Sub UpdateSearchHistory(ByVal SearchId As Integer, ByVal ModuleId As Integer, ByVal UserId As Integer, ByVal SearchText As String)
            DataProvider.Instance().UpdateSearchHistory(SearchId, ModuleId, UserId, SearchText)
        End Sub

        Public Function DoesSearchHistoryExist(ByVal UserId As Integer, ByVal SearchHistory As String) As Boolean
            Return DataProvider.Instance().DoesSearchHistoryExist(UserId, SearchHistory)
        End Function

        Public Sub DeleteSearchHistory(ByVal SearchId As Integer)
            DataProvider.Instance().DeleteSearchHistory(SearchId)
        End Sub

        Public Function GetSearchHistoryByUserId(ByVal UserId As Integer) As SearchHistoryInfoCollection
            Dim collSearch As New SearchHistoryInfoCollection
            collSearch.Fill(DataProvider.Instance().GetSearchHistoryByUserId(UserId))
            Return collSearch
        End Function

        Public Function GetSearchHistoryByModuleId(ByVal ModuleId As Integer) As SearchHistoryInfoCollection
            Dim collSearch As New SearchHistoryInfoCollection
            collSearch.Fill(DataProvider.Instance().GetSearchHistoryByModuleId(ModuleId))
            Return collSearch
        End Function

        Public Function GetSearchHistoryByModuleIdLike(ByVal ModuleId As Integer, ByVal SearchText As String) As SearchHistoryInfoCollection
            Dim collSearch As New SearchHistoryInfoCollection
            collSearch.Fill(DataProvider.Instance().GetSearchHistoryByModuleIdLike(ModuleId, SearchText))
            Return collSearch
        End Function

        Public Function GetSearchHistoryBySearchId(ByVal SearchId As Integer) As SearchHistoryInfo
            Dim objSearch As New SearchHistoryInfo
            objSearch.Fill(DataProvider.Instance().GetSearchHistoryBySearchId(SearchId))
            Return objSearch
        End Function

        Public Function GetSearchHistory() As SearchHistoryInfoCollection
            Dim collSearch As New SearchHistoryInfoCollection
            collSearch.Fill(DataProvider.Instance().GetSearchHistory)
            Return collSearch
        End Function

#End Region

        Public Shared Sub SaveCookie(ByVal Key As String, ByVal Value As Object)

            If Not HttpContext.Current Is Nothing And Not Value Is Nothing Then

                HttpContext.Current.Response.Cookies(Key).Value = Value.ToString
                HttpContext.Current.Response.Cookies(Key).Expires = DateTime.Now.AddDays(1)

            End If

        End Sub

#Region " IPortable Implementation "

        Public Function ExportModule(ByVal ModuleID As Integer) As String Implements DotNetNuke.Entities.Modules.IPortable.ExportModule
            Dim sb As New StringBuilder(150)
            Dim collSearch As New SearchHistoryInfoCollection
            collSearch = GetSearchHistoryByModuleId(ModuleID)
            Dim objSearch As New SearchHistoryInfo

            sb.Append("<WillStrohl><SearchHistory>")
            For Each obj As SearchHistoryInfo In collSearch
                sb.Append("<Search>")
                sb.AppendFormat("<SearchId>{0}</SearchId>", obj.SearchId)
                sb.AppendFormat("<ModuleId>{0}</ModuleId>", obj.ModuleId)
                sb.AppendFormat("<UserId>{0}</UserId>", obj.UserId)
                sb.AppendFormat("<SearchText>{0}</SearchText>", XmlUtils.XMLEncode(obj.SearchText))
                sb.Append("</Search>")
            Next
            sb.Append("</SearchHistory>")

            ' later on, will probably need to add module settings here

            sb.Append("</WillStrohl>")

            Return sb.ToString
        End Function

        Public Sub ImportModule(ByVal ModuleID As Integer, ByVal Content As String, ByVal Version As String, ByVal UserID As Integer) Implements DotNetNuke.Entities.Modules.IPortable.ImportModule
            Dim injContents As XmlNode = DotNetNuke.Common.Globals.GetContent(Content, "SearchHistory")

            For Each xmlLightbox As XmlNode In injContents.SelectNodes("Search")
                Dim objSearch As New SearchHistoryInfo
                objSearch.ModuleId = ModuleID
                objSearch.UserId = -1
                objSearch.SearchText = xmlLightbox.SelectSingleNode("SearchText").InnerText

                objSearch.SearchId = AddSearchHistory(objSearch.ModuleId, objSearch.UserId, objSearch.SearchText)

                ' later on, will probably need to add module settings here

            Next

        End Sub

#End Region

    End Class

End Namespace